package net.htjs.juc.reentrantlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;


/**
 * ReentrantLock可打断特性
 */
@Slf4j
public class ReentrantLockInterruptDemo {
    static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            try {
                lock.lockInterruptibly();
                log.info("get lock");
            } catch (InterruptedException e) {
                log.info("interrupt");
                return;
            } finally {
                log.info("unlock");
                lock.unlock();
            }
        }, "t1");
        try {
            log.info("get lock");
            lock.lock();
        } finally {
            log.info("unlock");
            lock.unlock();
        }

        t1.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t1.interrupt();
    }

}
